﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR.Hubs;
using System.Web.Security;
using System.IO;

namespace uffChat.Models
{
    public class MyTestChatHub : Hub
    {
        private static List<ChatMsg> msgsContainer = new List<ChatMsg>();

        public void SendMessage(string nick, string message)
        {
            lock(msgsContainer)
            {
                ChatMsg msg = new ChatMsg(nick,message,DateTime.Now);
                msgsContainer.Add(msg);
                string chatLogPath = String.Format("{0}\\{1}",AppDomain.CurrentDomain.BaseDirectory,"chatLog.txt");
                File.AppendAllText(chatLogPath,msg.ToString());
                this.Clients.addMessage(msg);
            }
        }

        public static IEnumerable<ChatMsg> GetMsgsFromGivenTime(int amountOfMinutesToSubstract)
        {
            lock (msgsContainer)
            {
                TimeSpan span = new TimeSpan(0, amountOfMinutesToSubstract, 0);
                return msgsContainer
                        .OrderBy(x => x.Time)
                        .Where(x => x.Time > Membership.GetUser().LastLoginDate.Subtract(span));
            }
        }
    }
}